
package com.tyae.relatorios.servico;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.query.JRHibernateQueryExecuterFactory;

import org.hibernate.Session;

import com.tyae.dao.HibernateUtil;

public class GeradorPDF implements Serializable, GeradorRelatorio{

	private static final long serialVersionUID = -3200822961269778067L;
	private String caminhoRelatorio;
	private Map<String, Object> parametros = new HashMap<String, Object>();

	public GeradorPDF() {
	}

	public GeradorPDF(String caminhoRelatorio, Map<String, Object> parametros) {
		this.caminhoRelatorio = caminhoRelatorio;
		this.parametros = parametros;
	}

	private static HttpServletResponse getServletResponse() {     
        return (HttpServletResponse)getFacesContext().getExternalContext().getResponse();     
    }

	private static FacesContext getFacesContext() {     
        return FacesContext.getCurrentInstance();     
    }

	@Override
	public void imprimir() throws JRException, IOException {
		HttpServletResponse res = getServletResponse();

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

	    try {
	    	parametros.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, session);
			InputStream reportStream = getClass().getResourceAsStream(getCaminhoRelatorio());

	        // Em PDF na propria pagina
	        byte[] bytes = JasperRunManager.runReportToPdf(reportStream, getParametros());

			if (bytes != null && bytes.length > 0) {
				res.setContentType("application/pdf");
				res.setContentLength(bytes.length);
				ServletOutputStream servletOutputStream = res.getOutputStream();
				getFacesContext().responseComplete();

				servletOutputStream.write(bytes);
				servletOutputStream.flush();
				servletOutputStream.close();
			}
		} finally {

		}
	}

	@Override
	public void gerarArquivo(String nomeArquivo) throws JRException, IOException {
		HttpServletResponse res = getServletResponse();

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

	    try {
	    	parametros.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, session);
			InputStream reportStream = getClass().getResourceAsStream(getCaminhoRelatorio());
			res.setContentType("application/pdf");
            res.setHeader("Content-disposition", "attachment;filename="+nomeArquivo+".pdf");
            ServletOutputStream servletOutputStream = res.getOutputStream();

            JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parametros);

            getFacesContext().responseComplete();   
            servletOutputStream.flush();   
            servletOutputStream.close(); 
        } catch (JRException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        } 
	}

	public String getCaminhoRelatorio() {
		return caminhoRelatorio;
	}

	@Override
	public void setCaminhoRelatorio(String caminhoRelatorio){
		this.caminhoRelatorio = caminhoRelatorio;
	}

	public Map<String, Object> getParametros(){
		return parametros;
	}

	public void setParametros(Map<String, Object> parametros){
		this.parametros = parametros;
	}

	@Override
	public void addParametro(String nomeParametro, Object valor){
		parametros.put(nomeParametro, valor);
	}
}